package com.gbb.simple.base;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gbb.common.utils.AppManager;
import com.gbb.simple.R;


/**
 * 具有Title的FragmentActivity
 */
public abstract class TitleFragmentActivity extends BaseFragmentActivity {

    protected Toolbar mToolbar;
    protected TextView titleView;

    protected LinearLayout contentLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_app_base);

        mToolbar = findViewById(R.id.widget_toolbar);
        setSupportActionBar(mToolbar);
        titleView = findViewById(R.id.toolbar_title);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        onCreateAfter(savedInstanceState);
        initTitleBar();
    }

    protected abstract void onCreateAfter(Bundle savedInstanceState);

    protected void initTitleBar() {
        mToolbar.setNavigationIcon(R.drawable.ic_action_back);
        mToolbar.setNavigationOnClickListener(getFinishListener());
    }

    @Override
    protected void onTitleChanged(CharSequence title,int color) {
        super.onTitleChanged(title,color);
        if(titleView != null) {
            titleView.setText(title);
        }
    }

    /**
     * 添加布局文件【默认有标题栏】
     *
     * @param resourceId 布局文件ID
     */
    protected void addLayout(int resourceId) {
        contentLayout = findViewById(R.id.content_layout);
        getLayoutInflater().inflate(resourceId,contentLayout);
    }

    public LinearLayout getContentLayout() {
        return contentLayout;
    }

    public OnClickListener getFinishListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                AppManager.getInstance().getCurrActivity().finish();
            }
        };
    }

}
